<?php namespace Phpcmf\Model\Quan;

// 模块内容模型类

class Content extends \Phpcmf\Model\Content {


    // 内容显示
    public function _call_show($data) {
        $tieid = (int)\Phpcmf\Service::L('input')->get('tieid');

        $url = '';
        $son = $this->table(SITE_ID.'_'.MOD_DIR.'_form_son')
            ->where('cid', $data['id'])
            ->where('status=1')
            ->order_by('displayorder asc,id asc')->getAll();
        $sontieid = $this->table(SITE_ID.'_'.MOD_DIR.'_form_son')
            ->where('id', $tieid)
            ->where('shenhe',1)
            //->get()->getRowArray();
            //->getAll();
            ->getRow();
            
        if ($son) {
            foreach ($son as $i => $t) {
                $son[$i]['url'] = dr_son_url($t);
                !$url && $url = $son[$i]['url'];
            }
        }
        /*dr_debug('t', $t);
        dr_debug('sontieid', $sontieid);*/

        $data['son_url'] = $url;
        
        //帖子tieid
        if($tieid) {
            
            $data['son_tie'] = $sontieid;
            $t['id'] = $tieid;
            $data['son_tie']['url'] = dr_son_url($t);
            $data['son_tie']['touxiang'] = dr_avatar($sontieid['uid']);
            //名称
            //$userweixin = $this->table('member_oauth')->where('uid', $sontieid['uid'])->getRow();
            $userweixin=\Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $sontieid['uid'])->get()->getRowArray();
            
            $user=dr_member_info($sontieid['uid']);
            dr_debug('userweixin', $userweixin);
            dr_debug('user', $user['group']);
            if ($user['name']) {
                $data['son_tie']['mingcheng'] = $user['name']; 
            } elseif ($userweixin['nickname']) {
                $data['son_tie']['mingcheng'] = $userweixin['nickname']; 
            } else {
                $data['son_tie']['mingcheng'] = $user['username'];
            }

            //用户组
            foreach ($user['group'] as $t) {
                $data['son_tie']['yonghuzu'] = $t['group_name'];
            }
            //归属地
            $ip=\Phpcmf\Service::L('ip')->address($sontieid['inputip']);
            
            if ($ip == "未知地区"){
                $data['son_tie']['guishudi'] = "中国";
            } else{
                $data['son_tie']['guishudi'] = $ip;
            }
            //内容转换
            $data['son_tie']['neirong'] = dr_code2html($sontieid['neirong']);
            
            
        } else {
            $data['son_list'] = $this->format_son_list($son, $data['son_type']);
        }
        $data['son_type'] = $this->format_son_type($data['son_type']);
        $data['son_type_first'] = array_key_first($data['son_type']);

        return $data;
    }

    // 阅读章节类别
    public function son_type($type) {

        $type = dr_string2array($type);
        if (!$type) {
            $type = [
                //0 => dr_lang('未命名'),
            ];
        }

        return $type;
    }

    // 前台显示阅读章节类别
    public function format_son_type($type) {

        $type = dr_string2array($type);
        if (!$type) {
            $type = [
                0 => '',
            ];
        }

        return $type;
    }

    // 加入分类
    public function add_son_type($index, $name) {

        $stype = $this->son_type($index['son_type']);
        if (!$stype) {
            $stype[1] = $name;
        } else {
            $stype[] = $name;
        }

        $this->table(dr_module_table_prefix(APP_DIR))->update($index['id'], [
            'son_type' => dr_array2string($stype)
        ]);

        return dr_return_data(1, 'ok');
    }
	// bug
	public function son_bug($type) {
	
	    $type = dr_string2array($type);
	    if (!$type) {
	        $type = [
	            //0 => dr_lang('未命名'),
	        ];
	    }
	
	    return $type;
	}
	// 加入bug
	public function add_son_bug($index, $name) {
	
	    $stype = $this->son_bug($index['bugtitle']);
	    if (!$stype) {
	        $stype[1] = $name;
	    } else {
	        $stype[] = $name;
	    }
	
	    $this->table(dr_module_table_prefix(APP_DIR).'_form_son')->update($index['id'], [
	        'bugtitle' => dr_array2string($stype)
	    ]);
	
	    return dr_return_data(1, 'ok');
	}

    // 格式化章节数据
    function format_son_list($list, $type) {

        if (!$list) {
            return [];
        }

        $type = dr_string2array($type);
        if (!$type) {
            $type = [
                0 => '',
            ];
        }

        $rt = [];
        foreach ($type as $tid => $name) {

            if (!isset($rt[$tid])) {
                $rt[$tid] = [];
            }

            if ($list) {
                foreach ($list as $i => $t) {
                    if ((int)$t['tid'] == $tid) {
                        $rt[$tid][] = $t;
                        unset($list[$i]);
                    }
                }
            }
        }


        return $rt;
    }
    // 帖子数据
    function format_son_tie($tie) {

        if (!$tie) {
            return [];
        }


        $rt = [];
        if ($tie) {
                foreach ($tie as $i => $t) {
                    if ((int)$t['tid'] == $tid) {
                        $rt[$tid][] = $t;
                        unset($tie[$i]);
                    }
                }
            }


        return $rt;
    }
}